<?php

namespace app\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\Project;
use app\models\User;
use app\models\ProjectCategory;

/**
 * ProjectSearch represents the model behind the search form of `app\models\Project`.
 */
class ProjectSearch extends Project
{   
    const PAGESIZE = 20;
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'progress'], 'integer'],
            [['name', 'email', 'own', 'brief_desc', 'web_link', 'app_link', 'certify', 'desc', 'logo_url', 'contract_addr', 'social_link', 'label', 'status', 'created_time', 'updated_time', 'username', 'big_cate'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {   

        $query = Project::find()->select('p.*,u.nickname as username')->from(Project::tableName().'as p')->innerJoin(User::tableName().'as u', '`u`.`uid` = `p`.`uid`');
        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query, 
            'pagination' => [
                'pageSize' => self::PAGESIZE,
            ],
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }
        
        // grid filtering conditions
        $query->andFilterWhere([
            'p.id' => $this->id,
            'p.big_cate' => $this->big_cate,
            'p.progress' => $this->progress,
            'p.status' => $this->status,
        ]);

        //创建时间筛选
        if ($this->created_time) {
            //处理时间范围
            $created_time_arr = explode(' - ', $this->created_time);

            $query->andFilterWhere([
                'between', 'created_time',strtotime($created_time_arr[0]), strtotime($created_time_arr[1])
            ]);
        }

        //更新时间筛选
        if ($this->updated_time) {
            //处理时间范围
            $updated_time_arr = explode(' - ', $this->updated_time);
            $query->andFilterWhere([
                'between', 'p.updated_time',strtotime($updated_time_arr[0]), strtotime($updated_time_arr[1])
            ]);
        }

        $query->andFilterWhere(['like', 'p.name', $this->name])
            ->andFilterWhere(['like', 'p.email', $this->email])
            ->andFilterWhere(['like', 'p.own', $this->own])
            ->andFilterWhere(['like', 'p.brief_desc', $this->brief_desc])
            ->andFilterWhere(['like', 'p.web_link', $this->web_link])
            ->andFilterWhere(['like', 'p.app_link', $this->app_link])
            ->andFilterWhere(['like', 'p.certify', $this->certify])
            ->andFilterWhere(['like', 'p.desc', $this->desc])
            ->andFilterWhere(['like', 'p.logo_url', $this->logo_url])
            ->andFilterWhere(['like', 'p.contract_addr', $this->contract_addr])
            ->andFilterWhere(['like', 'p.social_link', $this->social_link])
            ->andFilterWhere(['like', 'p.label', $this->label])
            ->andFilterWhere(['like', 'u.nickname', $this->getAttribute('username')]);

        //默认排序
        $dataProvider->sort->defaultOrder = [
            'created_time' => SORT_DESC,
        ];

        //新建排序字段
        $dataProvider->sort->attributes = [
            'id' => [
                'asc' => ['id' => SORT_ASC],
                'desc' => ['id' => SORT_DESC],
            ],
            'created_time' => [
                'asc' => ['created_time' => SORT_ASC],
                'desc' => ['created_time' => SORT_DESC],
            ],
            // 'updated_time' => [
            //     'asc' => ['updated_time' => SORT_ASC],
            //     'desc' => ['updated_time' => SORT_DESC],
            // ],
            // 'username' => [
            //     'asc' => ['username' => SORT_ASC],
            //     'desc' => ['username' => SORT_DESC],
            // ],
        ];

        return $dataProvider;
    }
}
